import React, { useRef, useEffect } from 'react';
import { PageContainer } from '@ant-design/pro-layout';
import ReactEcharts from 'echarts-for-react';

export default () => {
  const instance = useRef(null);
  const lineOption = () => {
    const option = {
      title: {
        text: '某站点用户访问来源',
        subtext: '纯属虚构',
        left: 'center',
      },
      tooltip: {
        trigger: 'item',
      },
      legend: {
        orient: 'vertical',
        left: 'left',
      },
      series: [
        {
          name: '访问来源',
          type: 'pie',
          radius: '50%',
          data: [
            { value: 1048, name: '搜索引擎' },
            { value: 735, name: '直接访问' },
            { value: 580, name: '邮件营销' },
            { value: 484, name: '联盟广告' },
            { value: 300, name: '视频广告' },
          ],
          emphasis: {
            itemStyle: {
              shadowBlur: 10,
              shadowOffsetX: 0,
              shadowColor: 'rgba(0, 0, 0, 0.5)',
            },
          },
        },
      ],
    };
    return option;
  };
  const onChartClick = (param, echarts) => {
    console.log(param, echarts);
  };
  const onChartLegendselectchanged = (param, echarts) => {
    console.log(param, echarts);
  };
  //   const resize = () => {
  //     instance.current && instance.current.echarts.resize();
  //   };
  useEffect(() => {
    console.log(instance.current);

    // window.addEventListener('resize', instance.current.echarts.resize());
  }, []);
  return (
    <PageContainer>
      <ReactEcharts
        ref={instance}
        option={lineOption()}
        onEvents={{
          click: onChartClick,
          mousedown: onChartClick,
          legendselectchanged: onChartLegendselectchanged,
        }}
      ></ReactEcharts>
    </PageContainer>
  );
};
